package com.shire.shire_interface.constants;

import com.shire.shire_interface.common.utils.StrUtils;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class Constants {
    public static final String CACHE_ADDRESS = "curAddress";
    public static final String CACHE_VARIABLES = "cache_variables";
    public static final int CACHE_DEFAULT_TIMES = 3600;	// 默认缓存时间
    public static final int CACHE_DAY_TIMES = 24*3600;	// 1天
    public static final int CACHE_THREE_DAYS_TIMES = 3*24*3600;	// 3天
    public static final int CACHE_HOUR_TIMES = 1*3600;	// 1小时
    public static final int CACHE_FOREVER_TIMES = 30*24*3600;	// 1月
    public static final int CACHE_FIVE_MINUTE_TIMES = 5*60;	// 5分钟

    /*
     * 公共状态
     */
    public enum CommonStatus {
        ENABLE("启用"), DISABLE("禁用"), DELETE("逻辑删除");
        private static final Map<Integer, CommonStatus> map = new HashMap<Integer, CommonStatus>();
        static {
            for (CommonStatus e : EnumSet.allOf(CommonStatus.class)) {
                map.put(e.ordinal(), e);
            }
        }

        public static boolean isEnable(Integer ordinal) {
            if(ordinal == null) return false;
            return ENABLE.ordinal() == ordinal.intValue();
        }

        private String title;

        private CommonStatus(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }
        public int getCode(){
            return ordinal();
        }
        public static String getTitle(Integer status) {
            CommonStatus statusEnum = get(status);
            return statusEnum == null ? "" : statusEnum.getTitle();
        }

        public static Map<Integer, CommonStatus> getMap() {
            return map;
        }
        public static CommonStatus get(Integer status) {
            return map.get(status);
        }
    }

    /**
     * 性别
     */
    public enum Sex {
        FEMALE(2,"女"), MALE(1,"男"), UNKNOWN(0,"未知");
        private String title;
        private int code;
        Sex(int code, String title){
            this.title = title;
            this.code = code;
        }
        public static Sex getEnum(String desc) {
            if(StringUtils.isBlank(desc)) {
                return null;
            }
            for(Sex sex : Sex.values()) {
                if(StringUtils.equals(sex.getTitle(), desc)) {
                    return sex;
                }
            }
            return null;
        }

        public static Sex get(Integer code){
            for (Sex e : EnumSet.allOf(Sex.class)) {
                if(e.getCode()==code) return e;
            }
            return UNKNOWN;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }
    /**
     * 邮件状态
     * @author Loong
     *
     */
    public enum MailType{
        TEXT("文本邮件"),HTML("HTML邮件");
        private String title;
        private MailType(String title){
            this.title = title;
        }
        public String getTitle() {
            return title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public void setTitle(String title) {
            this.title = title;
        }
    }
    /**
     * 文件类型
     * @author Loong
     *
     */
    public enum FileType{
        IMAGE("图片"),OFFICE("文档"),ZIP("压缩"),OTHER("其他");
        private String title;
        private FileType(String title){
            this.title = title;
        }

        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public static FileType pauseFileType(String ext)
        {
            String key=ext.toLowerCase();
            if(key.startsWith("."))
                key=key.substring(1);

            if(".jpg.gif.tif.png.bmp.ico.".indexOf("."+key+".")>=0)
                return IMAGE;
            else if(".doc.docx.xls.xlsx.txt.pdf.ppt.".indexOf("."+key+".")>=0)
                return OFFICE;
            else if(".zip.rar.7z.iso.".indexOf("."+key+".")>=0)
                return ZIP;
            else {
                return OTHER;
            }

        }
    }
    /**
     * 是否
     */
    public enum YesNo {
        YES("是"), NO("否");
        private static final Map<Integer, YesNo> map = new HashMap<Integer, YesNo>();
        static {
            for (YesNo e : EnumSet.allOf(YesNo.class)) {
                map.put(e.ordinal(), e);
            }
        }
        public static Map<Integer, YesNo> getMap() {
            return map;
        }
        public static boolean isEnable(Integer ordinal) {
            if(ordinal == null) return false;
            return YES.ordinal() == ordinal.intValue();
        }
        public int getCode(){
            return ordinal();
        }
        private String title;

        private YesNo(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

        public static String getTitle(Integer status) {
            YesNo yes = get(status);
            return yes == null ? "" : yes.getTitle();
        }

        public static YesNo get(Integer status) {
            if(status==null || status==NO.ordinal()) return NO;
            return YES;
        }

        public static boolean getBoolean(Integer status){
            if(status!=null && status==0) return true;
            return false;
        }
    }

    /**
     * 模块类别
     * @author Loong
     *
     */
    public enum Privilege {
        LIST("list", "列表"), DETAIL("detail", "详情"), ADD("add", "添加"),
        EDIT("edit", "编辑"), DELETE("delete", "删除"),
        FILE("file","导出文件"),
        EXPORT_EXCEL("excel", "导出EXCEL"),
        EXPORT_PDF("pdf", "导出PDF"), GRANT("grant", "授权"), PUSH("push","推送"),VERIFY("verify","审核"),
        GENERATE("generate","生成"),UPLOAD("upload", "上传"),CREATEAGAIN("createAgain", "重新生成"),
        SCREEN("screen", "屏幕"),LOGIN("login", "登录"),PASSWORD("password", "密码");
        private String title;
        private String name;
        private Privilege(String name, String title){
            this.name = name;
            this.title = title;
        }

        public String getName(){
            return name;
        }

        public String getTitle(){
            return title;
        }
        public int getCode(){
            return ordinal();
        }
        public static List<Privilege> getPrivileges(String privilegeStr){
            List<Privilege> list = new ArrayList<Privilege>();
            if(StrUtils.isBlank(privilegeStr)) return list;
            try{
                String[] priArray = privilegeStr.trim().split("@");
                for(String pri : priArray){
                    Privilege privilege = get(pri.trim());
                    if(privilege!=null){
                        list.add(privilege);
                    }
                }
            }
            catch (Exception e) {
            }
            return list;
        }

        public String getName(Integer ordinal){
            Privilege privilege = get(ordinal);
            if(privilege!=null){
                return privilege.getName();
            }
            return null;
        }

        public String getTitle(Integer ordinal){
            Privilege privilege = get(ordinal);
            if(privilege!=null){
                return privilege.getTitle();
            }
            return null;
        }

        public static Privilege get(String name) {
            if(name==null) return null;
            for(Privilege privilege : EnumSet.allOf(Privilege.class)){
                if(privilege.getName().equalsIgnoreCase(name)) return privilege;
            }
            return null;
        }

        public static Privilege get(Integer ordinal) {
            if(ordinal==null) return null;
            for(Privilege privilege : EnumSet.allOf(Privilege.class)){
                if(privilege.ordinal()==ordinal) return privilege;
            }
            return null;
        }
    }

    /**
     * 任务状态
     * @author xcb
     *
     */
    public enum JobStatus {
        WAITING("等待执行"), RUNNING("正在执行"), SUCCESS("执行成功"), FAILED("执行失败"), FREEZE("冻结"),DELETED("删除任务"),NEWJOB("新任务");
        private static final Map<Integer, JobStatus> map = new HashMap<Integer, JobStatus>();
        static {
            for (JobStatus e : EnumSet.allOf(JobStatus.class)) {
                map.put(e.ordinal(), e);
            }
        }

        private String title;

        private JobStatus(String title) {
            this.title = title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public String getTitle() {
            return title;
        }

        public static String getTitle(Integer status) {
            JobStatus statusEnum = get(status);
            return statusEnum == null ? "" : statusEnum.getTitle();
        }

        public static JobStatus get(Integer status) {
            return map.get(status);
        }
    }

    /**
     * 运营商
     * @author xcb
     *
     */
    public enum Carrier {
        CMCC("移动"), UNICOM("联通"), TELECOM("电信"), NONE("未知");
        private static final Map<Integer, Carrier> map = new HashMap<Integer, Carrier>();
        static {
            for (Carrier e : EnumSet.allOf(Carrier.class)) {
                map.put(e.ordinal(), e);
            }
        }
        private String title;
        public Integer getCode(){
            return ordinal();
        }
        private Carrier(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

        public static String getTitle(Integer ordi) {
            Carrier em = get(ordi);
            return em == null ? "" : em.getTitle();
        }

        public static Carrier get(Integer ordi) {
            return map.get(ordi);
        }
    }

//设备枚举冲突 考虑暂时未用到，且注释。
//	public enum DevType {
//		ADR("安卓"), IOS("苹果");
//		private String title;
//		private DevType(String title){
//			this.title = title;
//		}
//
//		public String getTitle(){
//			return title;
//		}
//
//		public static DevType get(int ordi){
//			for (DevType e : EnumSet.allOf(DevType.class)) {
//				if(e.ordinal()==ordi) return e;
//			}
//			return ADR;
//		}
//	}
    /**
     * 省市县
     */
    public enum AreaType {
        PROV("省"), CITY("市"), COUNTY("县区"), STREET("街");
        private String title;
        private AreaType(String title){
            this.title = title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public String getTitle(){
            return title;
        }

        public static AreaType get(int ordi){
            for (AreaType e : EnumSet.allOf(AreaType.class)) {
                if(e.ordinal()==ordi) return e;
            }
            return PROV;
        }
    }

    /**
     * 审核状态
     * @author Loong
     *
     */
    public enum VerifyStatus {
        WAITING("待审"), PASSED("通过"), REJECT("驳回");
        private String title;
        private VerifyStatus(String title){
            this.title = title;
        }

        public String getTitle(){
            return title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public static VerifyStatus get(int ordi){
            for (VerifyStatus e : EnumSet.allOf(VerifyStatus.class)) {
                if(e.ordinal()==ordi) return e;
            }
            return WAITING;
        }
    }

    public enum DeviceStatus {
        ENABLE("启用"), DISABLE("禁用"), DELETE("逻辑删除"), FAULT("故障"), REMOVE("移除"), MANUAL("手动模式");
        private String title;

        private DeviceStatus(String title){
            this.title = title;
        }

        public String getTitle(){
            return title;
        }
        public Integer getCode(){
            return ordinal();
        }
        public static DeviceStatus get(int ordi){
            for (DeviceStatus e : EnumSet.allOf(DeviceStatus.class)) {
                if(e.ordinal()==ordi) return e;
            }
            return ENABLE;
        }

    }
}
